﻿/*
 * This file is a part of the Smug# open source project.
 * 
 * Copyright (C) 2010, Troy Simpson 
 * 
 * Project Home: http://www.ebswift.com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.IO;
using System.Threading;
using System.Data.SQLite;
using System.Data;
using Ebswift;

namespace copy2smug
{
    //public delegate void SmugPhotoEventHandler(long id, string thumburl);

    /// <summary>
    /// Used to populated the SmugMug images database.
    /// </summary>
    class SmugMugImages
    {
        private static SmugMug m_smugmug;
        private static long m_albumid;
        private static Window1 m_w1; // reference to main UI
        public static List<SmugMugImage> PhotoList = new List<SmugMugImage>();
        private static bool running = false;
        private static long cancel = -1;

        public static event EventHandler Started; // started building the list
        public static event EventHandler Complete; // completed building the list

        protected void OnStarted(EventArgs e)
        {
            EventHandler myEvent = Started;

            if (myEvent != null)
            {

                myEvent(this, e);
            }
        }

        protected void OnComplete(List<FlickrPhoto> photo)
        {
            EventHandler myEvent = Complete;

            if (myEvent != null)
            {
                myEvent(this, EventArgs.Empty);
            }
        }

        /// <summary>
        /// Sets up the thread for loading photos from SmugMug to populate PhotoList.
        /// </summary>
        /// <param name="smugmug"></param>
        /// <param name="albumid"></param>
        /// <param name="w1"></param>
        public static void LoadPhotos(SmugMug smugmug, long albumid, Window1 w1)
        {
            if (running == true)
                cancel = albumid;

            running = true;

            m_smugmug = smugmug;
            m_albumid = albumid;
            m_w1 = w1;

            PhotoList = new List<SmugMugImage>();

            Thread t = new Thread(doLoadPhotos);
            t.Start();
        }

        /// <summary>
        /// Populates PhotoList from the database matching m_albumid.
        /// </summary>
        private static void doLoadPhotos()
        {
            m_w1.Dispatch(w =>
            {
                Started(null, EventArgs.Empty);
            });

            if (m_smugmug.Command.Connection.State != ConnectionState.Open)
                m_smugmug.Command.Connection.Open();

            SQLiteDataAdapter da = new SQLiteDataAdapter();

            // cache to database from SmugMug
            m_smugmug.DoSmugMug(new RESTType.GetAlbums());

            DataSet dsImages = new DataSet();
            SQLiteParameter userparam = new SQLiteParameter("@AlbumId", m_albumid);

            m_smugmug.Command.Parameters.Clear();
            m_smugmug.Command.Parameters.Add(userparam);
            m_smugmug.Command.CommandText = "Select id, AlbumId, ThumbURL From Image Where AlbumId=@AlbumId";
            da.SelectCommand = m_smugmug.Command;
            da.Fill(dsImages);
            dsImages.Tables[0].TableName = "Image";

            foreach (DataRow dr in dsImages.Tables[0].Rows)
            {
                // another thread has started on a new albumid
                if (cancel != -1 && cancel != m_albumid)
                {
                    cancel = -1;
                    m_smugmug.Command.Connection.Close();
                    return;
                }

                m_w1.Dispatch(w =>
                {
                    PhotoList.Add(new SmugMugImage((long)dr["id"], (string)dr["ThumbURL"]));
                });
            }

            m_smugmug.Command.Connection.Close();

            running = false;

            m_w1.Dispatch(w =>
            {
                Complete(null, null);
            });
        }
    }

    /// <summary>
    /// Defines a SmugMug image.
    /// </summary>
    class SmugMugImage
    {
        public long Id { get; set; }
        [System.Reflection.ObfuscationAttribute]
        public BitmapImage Thumb { get; set; }

        public SmugMugImage(long id, string thumb)
        {
            Id = id;
            Thumb = new BitmapImage(new Uri(thumb));
        }
    }

    /// <summary>
    /// Defines a SmugMug Album.
    /// </summary>
    class Album
    {
        public long Id { get; set; }
        public string Key { get; set; }

        public Album(long id, string key)
        {
            Id = id;
            Key = key;
        }
    }
}
